import pandas as pd
from scipy import stats

class DataProfiler:
    def __init__(self, df: pd.DataFrame):
        self.df = df

    def generate_report(self) -> dict:
        """生成数据质量报告"""
        report = {
            "missing_rates": self.df.isnull().mean().to_dict(),
            "data_types": self.df.dtypes.astype(str).to_dict(),
            "outlier_flags": self._detect_outliers()
        }
        return report

    def _detect_outliers(self) -> dict:
        """使用Z-score检测数值型异常值"""
        outliers = {}
        for col in self.df.select_dtypes(include='number'):
            z_scores = stats.zscore(self.df[col].dropna())
            outliers[col] = (abs(z_scores) > 3).sum()
        return outliers